<template>
  <div>
    <el-form ref="form" label-width="70px" :model="data" :rules="rules" class="all" :inline="true">
      <h3 class="title">系统登录</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="data.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="data.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-button type="primary" class="btn" @click="submit">登录</el-button>
    </el-form>
  </div>
</template>

<script>
import Mock from "mockjs"
import Cookie from "js-cookie"
import {getMenu} from "../api/index"
export default {
  data(){
    return {
      data:{
        username:'',
        password:'',
      },
      rules:{
        username:[{required:true,trigger: 'blur',message:'请输入用户名'}],
        password:[{required:true,trigger: 'blur',message:'请输入密码'}],
      }
    }
  },
  methods:{
    submit(){
      // // 模拟token信息
      // let token=Mock.Random.guid()
      // // 将token信息存入cookie
      // Cookie.set('token',token)
      // // 登录成功跳转首页
      // this.$router.push('/home')

      // 校验通过执行后续方法
      this.$refs.form.validate((val)=>{
        if(val){
          getMenu(this.data).then(({data})=>{
            console.log(data)
            if(data.code===20000){
              // 将token存入Cookie
              Cookie.set('token',data.data.token)
              //  获取的菜单数据，存入store中
              this.$store.commit('setMenu',data.data.menu)
              this.$store.commit('addMenu',this.$router)

              // 跳转首页
              this.$router.push('/home')
            }else{
              this.$message.error(data.data.message)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .all{
    width:350px;
    margin:180px auto;
    border:1px solid #eaeaea;
    border-radius:18px;
    box-sizing:border-box;
    padding: 35px 35px 15px 35px ;
    box-shadow:0 0 25px #cac6c6;
    background-color: #fff;
    .title{
      text-align:center;
      margin-bottom:40px;
      color:#505458;
    }
    .el-input{
      width:198px;
    }
    .btn{
      margin-left:105px;
      margin-top:10px;
    }
  }
</style>